package com.centronix.manager.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.centronix.bean.PurchaseVO;
import com.centronix.dao.ProductDAO;
import com.centronix.dao.PurchaseDAO;
import com.centronix.domain.Product;
import com.centronix.domain.Purchase;
import com.centronix.manager.PurchasesManager;

@Service(value="purchasesManager")
@Transactional
public class PurchasesManagerImpl implements PurchasesManager, Serializable{
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
    private PurchaseDAO purchasesDAO;
	
	@Autowired
	private ProductDAO productDAO;
	
	public List<PurchaseVO> findAllPurchase(){
		PurchaseVO purchaseVO = null;
		List<PurchaseVO> result = new ArrayList<PurchaseVO>();
		List<Purchase> purchases = purchasesDAO.findAll();
		for (Purchase purchase : purchases) {
			purchaseVO = new PurchaseVO();
			populatePurchaseToPurchaseVO(purchase, purchaseVO);
			result.add(purchaseVO);
		}
		return result;
	}
	
	private void populatePurchaseToPurchaseVO(Purchase purchase, PurchaseVO purchaseVO){
		purchaseVO.setBatchNo(purchase.getBatchNo());
		purchaseVO.setProduct(purchase.getProduct());
		purchaseVO.setCottonUnits(purchase.getCottonUnits());
		purchaseVO.setTotalUnits(purchase.getUnits());
		purchaseVO.setPurcahseDate(purchase.getPurcahseDate());
		purchaseVO.setPurchaseId(purchase.getPurchaseId());
		
		int units = purchase.getUnits();
		int cottonUnits = purchase.getCottonUnits();
		
		int noOfCottons = (int)Math.floor(units / cottonUnits);
		int remainingUnitsAfterCotton = units - (noOfCottons * cottonUnits);
		int noOfDozon = (int)Math.floor(remainingUnitsAfterCotton / 12);
		int remaningUnits = remainingUnitsAfterCotton - ( noOfDozon * 12);
		
		purchaseVO.setCotton(noOfCottons);
		purchaseVO.setDozon(noOfDozon);
		purchaseVO.setUnits(remaningUnits);
		
	}

	
	public Boolean savePurchase(PurchaseVO purchaseVO){
		Product product = productDAO.findOne(purchaseVO.getProductId());
		Purchase purchase = new Purchase();
		purchase.setPurchaseId(purchaseVO.getPurchaseId());
		purchase.setProduct(product);
		purchase.setBatchNo(purchaseVO.getBatchNo());
		purchase.setCottonUnits(product.getCottonUnits());
		purchase.setPurcahseDate(purchaseVO.getPurcahseDate());
		
		int totalUnits = 0;
		int dozenUnits = purchaseVO.getDozon() * 12;
		int cottonUnits = purchaseVO.getCotton() * product.getCottonUnits();
		totalUnits = dozenUnits + cottonUnits + purchaseVO.getUnits();
		purchase.setUnits(totalUnits);
		
		purchasesDAO.saveAndFlush(purchase);
		return true;
	}
	
	public Boolean deletePurchase(int purchasesId){
		purchasesDAO.delete(purchasesId);
		return true;
	}
	
	public PurchaseDAO getPurchasesDAO() {
		return purchasesDAO;
	}

	public void setPurchasesDAO(PurchaseDAO purchasesDAO) {
		this.purchasesDAO = purchasesDAO;
	}
	
	public List<Purchase> findAllPurchases() {
        return purchasesDAO.findAll();
    }
}
